const PAGE_CONF = require('../configs/page'),
      navData = require('../configs/nav'),
      bookTab = require('../configs/book_tab_data'),
      { IMG_BASE_URL } = require('../configs/url'),
      { searchData } = require('../libs/utils');

const { getSliderData } = require('../services/Slider'),
      { getRecomBookData } = require('../services/RecomBook'),
      { getPosterBookData } = require('../services/PosterBook'),
      { getStarAuthorData } = require('../services/Author'),
      { getBookData } = require('../services/AllBook');

class Home {
    async index (ctx, next) {
        const sliderData = await getSliderData(),
              posterBookData = await getPosterBookData(),
              recomBookData = await getRecomBookData(),
              bookData = await getBookData(),
              starAuthorData = await getStarAuthorData();
         
        await ctx.render('index.ejs', {// .ejs这个尾椎可以不写，会自动去匹配的
            PAGE_CONF: PAGE_CONF.INDEX,
            IMG_BASE_URL,
            navData,
            sliderData,
            posterBookData,
            recomBookData,
            bookData,
            starAuthorData,
        });
    }

    async list (ctx, next) {
        const keyword = ctx.params.kw,
              bookTabData = bookTab,
              bookData = await getBookData();

        await ctx.render('list', {
            PAGE_CONF: PAGE_CONF.LIST,
            IMG_BASE_URL,
            navData,
            bookTabData,
            bookData: keyword ? searchData(bookData, keyword) : bookData,
            bookDataStr: JSON.stringify(bookData)
        });
    }

    async error (ctx, next) {
        await ctx.render('error', {
            PAGE_CONF: PAGE_CONF.ERROR,
            navData
        });
    }

}

module.exports = new Home();